﻿using System.Collections.ObjectModel;
using System.Linq;
using WinRTFramework.Common;
using WinRTFramework.Interfaces.Contracts.Search;

namespace SearchContract.ViewModels
{
    class DefaultSearchablePageViewModel : Bindable, ISupportSearchActivation
    {
        private string[] _allStrings;
        private ObservableCollection<string> _filteredItems;

        public DefaultSearchablePageViewModel()
        {
            _allStrings = new[] {                 "Shanghai", "Istanbul", "Karachi", "Delhi", "Mumbai", "Moscow", "São Paulo", "Seoul", "Beijing", "Jakarta",
                "Tokyo", "Mexico City", "Kinshasa", "New York City", "Lagos", "London", "Lima", "Bogota", "Tehran", "Ho Chi Minh City",
                "Hong Kong", "Bangkok", "Dhaka", "Cairo", "Hanoi", "Rio de Janeiro", "Lahore", "Chonquing", "Bengaluru", "Tianjin",
                "Baghdad", "Riyadh", "Singapore", "Santiago", "Saint Petersburg", "Surat", "Chennai", "Kolkata", "Yangon", "Guangzhou",
                "Alexandria", "Shenyang", "Hyderabad", "Ahmedabad", "Ankara", "Johannesburg", "Wuhan", "Los Angeles", "Yokohama",
                "Abidjan", "Busan", "Cape Town", "Durban", "Pune", "Jeddah", "Berlin", "Pyongyang", "Kanpur", "Madrid", "Jaipur",
                "Nairobi", "Chicago", "Houston", "Philadelphia", "Phoenix", "San Antonio", "San Diego", "Dallas", "San Jose",
                "Jacksonville", "Indianapolis", "San Francisco", "Austin", "Columbus", "Fort Worth", "Charlotte", "Detroit",
                "El Paso", "Memphis", "Baltimore", "Boston", "Seattle Washington", "Nashville", "Denver", "Louisville", "Milwaukee",
                "Portland", "Las Vegas", "Oklahoma City", "Albuquerque", "Tucson", "Fresno", "Sacramento", "Long Beach", "Kansas City",
                "Mesa", "Virginia Beach", "Atlanta", "Colorado Springs", "Omaha", "Raleigh", "Miami", "Cleveland", "Tulsa", "Oakland",
                "Minneapolis", "Wichita", "Arlington", " Bakersfield", "New Orleans", "Honolulu", "Anaheim", "Tampa", "Aurora",
                "Santa Ana", "St. Louis", "Pittsburgh", "Corpus Christi", "Riverside", "Cincinnati", "Lexington", "Anchorage",
                "Stockton", "Toledo", "St. Paul", "Newark", "Greensboro", "Buffalo", "Plano", "Lincoln", "Henderson", "Fort Wayne",
                "Jersey City", "St. Petersburg", "Chula Vista", "Norfolk", "Orlando", "Chandler", "Laredo", "Madison", "Winston-Salem",
                "Lubbock", "Baton Rouge", "Durham", "Garland", "Glendale", "Reno", "Hialeah", "Chesapeake", "Scottsdale",
                "North Las Vegas", "Irving", "Fremont", "Irvine", "Birmingham", "Rochester", "San Bernardino", "Spokane",
                "Toronto", "Montreal", "Vancouver", "Ottawa-Gatineau", "Calgary", "Edmonton", "Quebec City", "Winnipeg", "Hamilton" };

            FilteredItems = new ObservableCollection<string>(_allStrings);            
        }

        public ObservableCollection<string> FilteredItems
        {
            get { return _filteredItems; }
            set { _filteredItems = value; NotifyPropertyChanged(() => FilteredItems); }
        }

        public void ProcessActivationQueryText(string language, string queryText)
        {
            ProcessQueryText(language, queryText);
        }

        private void ProcessQueryText(string language, string queryText)
        {
            FilteredItems = new ObservableCollection<string>(_allStrings.Where(k => k.StartsWith(queryText)));
        }

        public void ProcessQueryText(Windows.ApplicationModel.Search.SearchPaneQuerySubmittedEventArgs args)
        {
            ProcessQueryText(args.Language, args.QueryText);
        }

        public void OnSearchPaneSuggestionsRequested(Windows.ApplicationModel.Search.SearchPaneSuggestionsRequestedEventArgs args)
        {
            args.Request.SearchSuggestionCollection.AppendQuerySuggestions(_allStrings.Where(k => k.StartsWith(args.QueryText)));
        }
    }
}
